package com.zyp.messageboard.controller;

import com.zyp.messageboard.entity.Message;
import com.zyp.messageboard.entity.User;
import com.zyp.messageboard.service.MessageService;
import com.zyp.messageboard.service.impl.MessageServiceImpl;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet("/addMessage")
public class AddMessageServlet extends HttpServlet {

    private final MessageService messageService = new MessageServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title = request.getParameter("title");
        // 获取过滤后的内容
        String content = (String) request.getAttribute("filteredContent");

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setCreatedAt(LocalDateTime.now());
        message.setUser(user);

        messageService.save(message);
        response.sendRedirect("messageList");
    }
}
